<template>
  <el-form v-bind="$attrs" :model="model" ref="formRef">
    <el-row>
      <el-col v-for="item in schema" :key="item.key" :span="item.span">
        <el-form-item :label="item.label" :prop="item.model">
          <template v-if="item.component==='input'">
            <el-input v-model="model[item.model]" v-bind="item.props"></el-input>
          </template>
          <template v-else-if="item.component==='select'">
            <el-select v-model="model[item.model]" v-bind="item.props">
              <el-option 
                v-for="option in item.selectOptions"
                :key="option.value"
                :label="option.label"
                :value="option.value"
              />
            </el-select>
          </template>
          <template v-else-if="item.component==='submit-button'">
            <el-button v-bind="item.props" @click="submit(item.handleSubmit)">{{ item.btnText }}</el-button>
          </template>
          <template v-else-if="item.component==='button'">
            <el-button v-bind="item.props||{}" v-on="item.listeners||{}">{{ item.btnText }}</el-button>
          </template>
          
        </el-form-item>
      </el-col>
    </el-row>
  </el-form>
</template>

<script lang="ts">
export default {
  name: 'XForm',
}
</script>

<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import { ref } from 'vue'


  defineProps<{
    schema: any[]
    model: any
  }>()

  const formRef = ref<FormInstance>()

  const submit = async (callback: () => {}) => {
    // 进行表单校验
    await formRef.value?.validate()
    // 如果通过了, 调用传入的回调来通知父组件
    callback()
  }

</script>

<style scoped>

</style>
